/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fhv.itb.mst2955.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import fhv.itb.mst2955.client.controllers.ContactController;
import fhv.itb.mst2955.client.model.AddressProxy;
import fhv.itb.mst2955.client.model.ChangeObject;
import java.util.Vector;


/**
 * A dialog box for resolving synchronization conflicts.
 */
public class HandleConflictsDialog extends DialogBox implements ClickHandler {
    ContactController controller = ContactController.getInstance();
    Vector<ChangeObject> conflictObjects = controller.getSyncConflicts();

    public HandleConflictsDialog() {
        super();
        setTitle("Resolve Conflicts");

        FlexTable conflictsTable = new FlexTable();
        conflictsTable.addStyleName("FlexTable");

        Button saveButton = new Button("Save");
        saveButton.addClickHandler(this);

        conflictsTable.setText(0, 0, "Local");
        conflictsTable.setText(0, 1, "Remote");

        add(conflictsTable);

        int rows = 1;
        for (ChangeObject uo : conflictObjects) {
            conflictsTable.getRowFormatter().addStyleName(rows, "FlexTable-OddRow");

            FlexTable localTable = new FlexTable();
            FlexTable remoteTable = new FlexTable();

            if (uo.getObjectType().equals(ChangeObject.TYPE_ADDRESS)) {
                AddressProxy la = controller.getAddresses().get(uo.getObjectId());
                if (la != null) {
                    RadioButton localButton = new RadioButton("KeepButtonGroup", "keep local ");
                    localButton.setFormValue(ChangeObject.TYPE_ADDRESS + uo.getObjectId() + "local");

                    localTable.setText(0, 0, "ADDRESS:");
                    localTable.setText(1, 0, "Street:"); localTable.setText(1, 1, la.getStreet());
                    localTable.setText(2, 0, "Country:"); localTable.setText(2, 1, la.getCountryNickName());
                    localTable.setText(3, 0, "Zip:"); localTable.setText(3, 1, la.getZip());
                    localTable.setText(4, 0, "City:"); localTable.setText(4, 1, la.getCity());
                    localTable.setWidget(5, 0, localButton);
                    conflictsTable.setWidget(rows, 0, localTable);

                    AddressProxy ra = (AddressProxy) uo.getUpdateObject();
                    RadioButton remoteButton = new RadioButton("KeepButtonGroup", "keep remote ");
                    remoteButton.setFormValue(ChangeObject.TYPE_ADDRESS + uo.getObjectId() + "remote");
                    remoteTable.setText(0, 0, "ADDRESS:");
                    remoteTable.setText(1, 0, "Street:"); remoteTable.setText(1, 1, ra.getStreet());
                    remoteTable.setText(2, 0, "Country:"); remoteTable.setText(2, 1, ra.getCountryNickName());
                    remoteTable.setText(3, 0, "Zip:"); remoteTable.setText(3, 1, ra.getZip());
                    remoteTable.setText(4, 0, "City:"); remoteTable.setText(4, 1, ra.getCity());
                    remoteTable.setWidget(5, 0, remoteButton);
                    conflictsTable.setWidget(rows, 1, remoteTable);
                } else {
                    Window.alert("could not find local address");
                }
            } else { // address

            }
            rows++;
        }

        conflictsTable.setWidget(rows, 0, saveButton);
        show();
    }


    public void onClick(ClickEvent event) {
        // TODO save the right stuff

        hide();
    }

    
}
